/*
 * ------------------------------------------------------------------
 * Copyright © 2017 Hangzhou DtDream Technology Co.,Lt d. All rights reserved.
 * ------------------------------------------------------------------
 *       Product:
 *   Module Name:
 *  Date Created: 2022/9/27
 *   Description:
 * ------------------------------------------------------------------
 * Modification History
 * DATE            Name           Description
 * ------------------------------------------------------------------
 * 2022/9/27    小谷 g2038          created
 * ------------------------------------------------------------------
 */
package com.gujh.java.jmx.client;

import javax.management.remote.JMXConnector;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

public class JmxClientBuilder {

    private final JMXServiceURL jmxServiceUrl;

    private final Map<String, Object> environments = new HashMap<>();

    private JmxClientBuilder(String jmxServiceUrl) throws MalformedURLException {
        this.jmxServiceUrl = new JMXServiceURL(jmxServiceUrl);
    }

    public static JmxClientBuilder jmxServiceUrl(String jmxServiceUrl) throws MalformedURLException {
        return new JmxClientBuilder(jmxServiceUrl);
    }

    public final JmxClientBuilder addEnvironments(String key, Object value) {
        this.environments.put(key, value);
        return this;
    }

    public final JmxClientBuilder addCredentials(String user, String auth) {
        this.environments.put(JMXConnector.CREDENTIALS, new String[]{user, auth});
        return this;
    }


    public JmxClient build() throws IOException {
        return new JmxClient(jmxServiceUrl, environments);
    }
}
